#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_USERS 100
#define MAX_ITEMS 100
#define MAX_CART_ITEMS 10
#define MAX_ORDERS 100

// 用户结构
typedef struct {
    char username[50];  //用户名
    char password[50];  //密码
} User;

// 商品结构
typedef struct {
    int id;             //商品号
    char name[100];     //商品名
    float price;        //价格
} Item;

// 购物车结构
typedef struct {
    Item item;          //商品号
    int quantity;       //数量
} CartItem;

// 订单结构
typedef struct {
    int orderId;                              //订单号
    CartItem items[MAX_CART_ITEMS];           //订单商品
    int itemCount;                            //商品数
    float totalAmount;                        //总金额
} Order;


// 全局变量
User users[MAX_USERS];
int userCount = 0;

Item items[MAX_ITEMS] = {
    {1, "书籍",  30.0},
	{2, "手机",1150.0},
	{3, "手表", 200.0},
	{4, "台灯",  50.0},
	{5, "化妆镜",10.0},
	{6, "网球拍",60.0}
};
int itemCount = 6;

Order orders[MAX_ORDERS];
int orderCount = 0;

CartItem shoppingCart[MAX_CART_ITEMS];
int cartItemCount = 0;




//用户注册
void registerUser() {
    if (userCount >= MAX_USERS) {
        printf("用户数量已达上限。\n");
        return;
    }
    
    char username[50], password[50];
    printf("请输入用户名: ");
    scanf("%s", username);
    printf("请输入密码: ");
    scanf("%s", password);
    
    // 检查用户名是否已存在
    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0) {
            printf("该用户名已被注册。\n");
            return;
        }
    }
    
    // 添加新用户
    strcpy(users[userCount].username, username);
    strcpy(users[userCount].password, password);
    userCount++;
    printf("注册成功！\n");
}


//用户登录
int loginUser() {
    char username[50], password[50];
    printf("请输入用户名: ");
    scanf("%s", username);
    printf("请输入密码: ");
    scanf("%s", password);
    
    for (int i = 0; i < userCount; i++) {
        if (strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
            printf("登录成功！\n");
            return i; // 返回用户索引
        }
    }
    
    printf("用户名或密码错误。\n");
    return -1;
}


//显示商品列表
void displayItems() {
    printf("\n可购买的商品列表:\n");
    for (int i = 0; i < itemCount; i++) {
        printf("%d. %s - ￥%.2f\n", items[i].id, items[i].name, items[i].price);
    }
}


//添加到购物车
void addItemToCart() {
    int itemId, quantity;
    displayItems();
    printf("请输入要添加到购物车的商品ID: ");
    scanf("%d", &itemId);
    printf("请输入数量: ");
    scanf("%d", &quantity);

    // 检查商品ID是否有效
    int itemIndex = -1;
    for (int i = 0; i < itemCount; i++) {
        if (items[i].id == itemId) {
            itemIndex = i;
            break;
        }
    }
    if (itemIndex == -1) {
        printf("无效的商品ID。\n");
        return;
    }

    // 添加到购物车
    if (cartItemCount < MAX_CART_ITEMS) {
        shoppingCart[cartItemCount].item = items[itemIndex];
        shoppingCart[cartItemCount].quantity = quantity;
        cartItemCount++;
        printf("已添加到购物车。\n");
    } else {
        printf("购物车已满。\n");
    }
}


//结算和下单
void checkoutAndOrder() {
    if (cartItemCount == 0) {
        printf("购物车为空。\n");
        return;
    }

    float totalAmount = 0;
    printf("\n您的购物车:\n");
    for (int i = 0; i < cartItemCount; i++) {
        float itemTotal = shoppingCart[i].item.price * shoppingCart[i].quantity;
        printf("%d. %s - %d x ￥%.2f = ￥%.2f\n", i + 1, shoppingCart[i].item.name, shoppingCart[i].quantity, shoppingCart[i].item.price, itemTotal);
        totalAmount += itemTotal;
    }
    printf("总金额: ￥%.2f\n", totalAmount);

    // 生成订单
    if (orderCount < MAX_ORDERS) {
        Order newOrder;
        newOrder.orderId = orderCount + 1;
        memcpy(newOrder.items, shoppingCart, sizeof(shoppingCart[0]) * cartItemCount);
        newOrder.itemCount = cartItemCount;
        newOrder.totalAmount = totalAmount;
        orders[orderCount++] = newOrder;
        cartItemCount = 0; // 清空购物车
        printf("订单已生成，订单号: %d\n", newOrder.orderId);
    } else {
        printf("无法生成更多订单。\n");
    }
}


//查看订单
void viewOrders() {
    if (orderCount == 0) {
        printf("没有订单。\n");
        return;
    }

    for (int i = 0; i < orderCount; i++) {
        printf("\n订单号: %d\n", orders[i].orderId);
        for (int j = 0; j < orders[i].itemCount; j++) {
            printf("%d. %s - %d x ￥%.2f\n", j + 1, orders[i].items[j].item.name, orders[i].items[j].quantity, orders[i].items[j].item.price);
        }
        printf("总金额: ￥%.2f\n", orders[i].totalAmount);
    }
}


int main() {
    int loggedInUserIndex = -1; // 当前登录的用户索引
    int choice;

    while (1) {
        printf("\n***************************Welcome to Campus second-hand trading platform system!********************************\n");
        printf("1. 用户注册\n");
        printf("2. 用户登录\n");
        printf("3. 显示商品列表\n");
        printf("4. 添加商品到购物车\n");
        printf("5. 结算和下单\n");
        printf("6. 查看订单\n");
        printf("0. 退出\n");
        
        printf("请输入选项: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                registerUser();
                break;
            case 2:
                loggedInUserIndex = loginUser();
                break;
            case 3:
                displayItems();
                break;
            case 4:
                if (loggedInUserIndex != -1) {
                    addItemToCart();
                } else {
                    printf("请先登录。\n");
                }
                break;
            case 5:
                if (loggedInUserIndex != -1) {
                    checkoutAndOrder();
                } else {
                    printf("请先登录。\n");
                }
                break;
            case 6:
                if (loggedInUserIndex != -1) {
                    viewOrders();
                } else {
                    printf("请先登录。\n");
                }
                break;
            case 0:
                printf("感谢使用，再见！\n");
                return 0;
            default:
                printf("无效选项。\n");
        }
    }
    
    return 0;
}